#!/bin/bash
GCC_URL="http://euleros-release.rnd.huawei.com/zyh/gcc_make_perl_update_file/"
OBS_SRC_URL="http://10.175.100.158/SOURCE_DELIVERY/suse12sp2-obs2.9.5/"
OBS_TAR_9="OBSServer2.9.5.tar.gz"
OBS_TAR_7="OBSServer2.7.3.tar.gz"
OBS_SP4_SER_ISO="SLE-12-SP2-Server-DVD-x86_64-GM-DVD1.iso"
OBS_SP4_SDK_ISO="SLE-12-SP2-SDK-DVD-x86_64-GM-DVD1.iso"
OBS_INSTALL_TAR="obs_install.tar.gz"
PRE_PERL_SH="preperl.sh"

# get one file from $OBS_SRC_URL
function get_one_file()
{
    file=$1
    wget ${OBS_SRC_URL}${file} &> /dev/null
    if [ ! -f ${file} ]; then
        echo "not find ${file}"
        exit 1
    fi
}
# get obs source
function get_obs_tar()
{
    get_one_file ${OBS_TAR_9}
    get_one_file ${OBS_TAR_7}
    get_one_file ${OBS_SP4_SER_ISO}
    get_one_file ${OBS_SP4_SDK_ISO}
    get_one_file ${OBS_INSTALL_TAR}
    get_one_file ${PRE_PERL_SH}
}

#update service files
function update_obs_service()
{
    mkdir -p update
    pushd update
    wget http://10.175.100.158/SOURCE_DELIVERY/suse12sp2-obs2.7.3/obs_install.tar.gz &> /dev/null
    tar xf obs_install.tar.gz
    cp -f  obs_install/obs_install/config/service/* /usr/lib/obs/service
    mkdir -p /usr/lib/obs/source_md5
    chmod 777 /usr/lib/obs/source_md5
    mkdir -p /etc/obs/services
    echo CACHEDIRECTORY="/srv/cache/obs/tar_scm" > /etc/obs/services/tar_scm
    mkdir -p /srv/cache/obs/tar_scm && cd /srv/cache/obs/tar_scm && mkdir -p repo{,url} incoming
    rcobsservice restart
    popd
    systemctl disable obssigner.service
    systemctl stop obssigner.service
}
# install obs
function install_obs()
{
    get_obs_tar
    sed -i 's/-zxvf/-zxf/g' preperl.sh
    bash preperl.sh
    tar -zxf $OBS_INSTALL_TAR
    sed -i "/#Run obs setup wizard/i\    sed +e" /opt/obs_install/obs2.9.5_install.sh
    sed -i "/#Run obs setup wizard/a\    mysqladmin -u root password \"opensuse\"" /opt/obs_install/obs2.9.5_install.sh
    sed -i "/#Run obs setup wizard/a\    systemctl restart mysql" /opt/obs_install/obs2.9.5_install.sh
    echo |  bash -x /opt/obs_install/obs2.9.5_install.sh obs_server_install x86
}

# install gcc and other tools
function install_gcc()
{
    src_list="Python-3.7.0.tgz cmake-3.13.0-rc2.tar.gz gcc-7.3.0.tar.gz gmp-6.1.0.tar.bz2 install.sh isl-0.16.1.tar.bz2 make-4.2.1.tar.gz mpc-1.0.3.tar.gz mpfr-3.1.4.tar.bz2 my_prerequisites perl-5.28.0.tar.gz ruby-2.5.3.tar.gz zlib-1.2.11.tar.gz"
    wget -r -q -nd -np -A '*tar*' $GCC_URL &> /dev/null
    wget -r -q -nd -np -A '*sh' $GCC_URL &> /dev/null
    wget -r -q -nd -np -A 'my*' $GCC_URL &> /dev/null
    wget -r -q -nd -np -A 'Python*' $GCC_URL &> /dev/null
    for file in $src_list
    do
        if [ ! -f "$file" ]; then
            echo "not find $file"
            exit 1
        fi
    done
    sed -i 's/-zxvf/-zxf/g' install.sh
    bash -x install.sh
}
install_gcc
install_obs
update_obs_service
